<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>属性值列表</title>
    <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/layui@2.6.8/dist/css/layui.css">
    <script src="//cdn.jsdelivr.net/npm/layui@2.6.8/dist/layui.js" charset="utf-8"></script>
</head>

<body>
    <div style="margin: 15px;padding:15px;background-color: #fff;">
        <div style="text-align: right;">
            <a class="layui-btn layui-btn-xs layui-btn-primary" href="/admins/products/property_names">返回</a>
            <button class="layui-btn layui-btn-xs" onclick="add({$name_id})">添加</button>
        </div>

        <table class="layui-table">
            <thead>
                <tr>
                    <td>ID</td>
                    <td>属性名称</td>
                    <td>属性值</td>
                    <td>操作</td>
                </tr>
            </thead>

            <tbody>
                {if $items}
                {foreach $items as $item}
                <tr>
                    <td>{$item['id']}</td>
                    <td>{$item['name_title']}</td>
                    <td>{$item['title']}</td>
                    <td>
                        <button class="layui-btn layui-btn-xs" onclick="edit({$item['id']})">编辑</button>
                        <button class="layui-btn layui-btn-xs layui-btn-danger" onclick="del({$item['id']})">删除</button>
                    </td>
                </tr>
                {/foreach}
                {else /}
                <tr>
                    <td colspan="4" style="text-align: center;">暂无记录</td>
                </tr>
                {/if}
            </tbody>
        </table>

        <div id="pages"></div>
    </div>

    <script>
        layui.use('laypage', function () {
            var laypage = layui.laypage;

            // 分页
            laypage.render({
                elem: 'pages',
                count: '{$total}',
                limit: '{$pageSize}',
                curr: '{$page}',
                layout: ['prev', 'page', 'next', 'limit'],
                jump: function (obj, first) {
                    if (!first) {
                        window.location.href = '?page=' + obj.curr + '&limit=' + obj.limit;
                    }
                }
            });
        });

        var $ = layui.jquery;
        // 添加属性值
        function add(name_id) {
            layer.open({
                title: '添加属性值',
                type: 2,
                maxmin: true,
                area: ['480px', '450px'],
                content: '/admins/products/add_property_value?name_id=' + name_id
            });
        }

        // 修改属性值
        function edit(id) {
            layer.open({
                title: '修改属性值',
                type: 2,
                maxmin: true,
                area: ['480px', '450px'],
                content: '/admins/products/edit_property_value?id=' + id
            });
        }

        // 删除属性值
        function del(id) {
            layer.confirm('确定要删除这条吗？', { icon: 3 }, function () {
                $.post('/admins/products/del_property_value', { id }, function (res) {
                    if (res.code) return layer.alert(res.msg, { icon: 2 });
                    layer.alert(res.msg, { icon: 1 });
                    setTimeout(() => window.location.reload(), 1000);
                }, 'json')
            });
        }
    </script>
</body>

</html>